﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace W5_DoAnCuoiKy_Glow
{
    public class DataProvider
    {
        static string cnStr = ConfigurationManager.ConnectionStrings["QLSanDauGiacnStr"].ConnectionString;

        public static SqlConnection Connect()
        {
            SqlConnection cn = new SqlConnection(cnStr);
            return cn;
        }

        public static DataTable LoadSQL(string sql)
        {            
            SqlConnection cn = new SqlConnection(cnStr);
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            DataTable dt = new DataTable();

            da.Fill(dt);

            return dt;            
        }

        public static void ExcuteQuery(string sql)
        {
         
            SqlConnection cn = new SqlConnection(cnStr);           
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }

        public static DataTable LoadSQLProc(string sql, List<SqlParameter> para)
        {         
            SqlConnection cn = new SqlConnection(cnStr);
            SqlDataAdapter da = new SqlDataAdapter(sql,cn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            if (para != null)
            {
                foreach (SqlParameter temp in para)
                {
                    da.SelectCommand.Parameters.Add(temp);
                }
            }            
            DataTable dt = new DataTable();

            da.Fill(dt);

            return dt;
        }
        public static void ExcuteQueryProc(string sql, List<SqlParameter> para)
        {         
            SqlConnection cn = new SqlConnection(cnStr);
            SqlCommand cmd = new SqlCommand(sql,cn);

            cmd.CommandType = CommandType.StoredProcedure;

            foreach (SqlParameter temp in para)
            {
                cmd.Parameters.Add(temp);
            }

            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();        

        }
    }
}
